package gk.example.activity;

import gk.example.App;
import gk.example.BaseActivity;
import gk.example.bean.User;
import gk.example.util.ErrorCodeUtil;
import gk.example.util.T;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.listener.SaveListener;

import com.example.gkdate.R;

/**
 * 
 * Description:注册页面
 * 
 * @author Wenk.Chan（最新修改者）
 * @version 1.0（最新版本号）
 */
public class RegisterActivity extends BaseActivity implements OnClickListener {
	private EditText mEtAccount;
	private EditText mEtPassword;
	private EditText mEtRepeatPassword;
	private EditText mEtContact;
	private EditText mEtNickName;
	private TextView mTvTitle;
	private Button mBtnRegister;
	private CheckBox mCbShowPassWord;
	private ImageButton mBtnBack;

	@Override
	protected void initView() {
		setContentView(R.layout.activity_register);
		mEtAccount = (EditText) findViewById(R.id.et_register_account);
		mEtPassword = (EditText) findViewById(R.id.et_register_password);
		mEtRepeatPassword = (EditText) findViewById(R.id.et_register_repeat_password);
		mEtContact = (EditText) findViewById(R.id.et_register_contact);
		mEtNickName = (EditText) findViewById(R.id.et_register_nickname);
		mTvTitle = (TextView) findViewById(R.id.tv_top_title);
		mBtnRegister = (Button) findViewById(R.id.btn_register);
		mCbShowPassWord = (CheckBox) findViewById(R.id.cb_register_show_password);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);

	}

	@Override
	protected void initEvent() {
		mEtContact.setEnabled(false);
		mEtContact.setText(App.BmobUser.getContact());
		mBtnRegister.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
		mTvTitle.setText(getResources().getString(R.string.register));
		mCbShowPassWord
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT
									| InputType.TYPE_TEXT_VARIATION_NORMAL);
							mEtRepeatPassword
									.setInputType(InputType.TYPE_CLASS_TEXT
											| InputType.TYPE_TEXT_VARIATION_NORMAL);
						} else {
							mEtPassword
									.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD
											| InputType.TYPE_CLASS_TEXT);
							mEtRepeatPassword
									.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD
											| InputType.TYPE_CLASS_TEXT);
						}

					}
				});
	}

	/**
	 * 
	 * 描述：进行注册相关操作
	 * 
	 * @return void
	 */
	private void register() {
		String account = mEtAccount.getText().toString();
		String nickname = mEtNickName.getText().toString();
		String password = mEtPassword.getText().toString();
		String repeatPassword = mEtRepeatPassword.getText().toString();
		String contact = mEtContact.getText().toString();
		if (account.equals("") || password.equals("")
				|| repeatPassword.equals("") || contact.equals("")) {
			T.show(this, "请完整填写注册信息", Toast.LENGTH_SHORT);
			return;
		} else if (account.length() < 5) {
			T.show(this, "账号不能少于五位", Toast.LENGTH_SHORT);
			return;
		} else if (!account.substring(0, 1).matches("^[A-Za-z]+$")) {
			T.show(this, "账号首位非字母", Toast.LENGTH_SHORT);
			return;
		} else if (!account.matches("^\\w+$")) {
			T.show(this, "账号不能为字母,数字,下划线以外的字符", Toast.LENGTH_SHORT);
			return;
		} else if (nickname.getBytes().length > 18) {
			T.show(this, "昵称过长,请修改", Toast.LENGTH_SHORT);
			return;
		} else if (password.length() < 6) {
			T.show(this, "密码不能少于六位", Toast.LENGTH_SHORT);
			return;
		} else if (password.matches("^[\u4E00-\u9FA5]+$")
				|| repeatPassword.matches("^[\u4E00-\u9FA5]+$")) {
			T.show(this, "密码不能含有中文", Toast.LENGTH_SHORT);
			return;
		} else if (!password.equals(repeatPassword)) {
			T.show(this, "两次输入密码不一致", Toast.LENGTH_SHORT);
			return;
		}
		final User user = new User();
		user.setUsername(account);
		user.setNickName(nickname);
		user.setPassword(repeatPassword);
		user.setSex("男");
		user.setContact(contact);
		user.setIntall(BmobInstallation.getInstallationId(this));
		user.setEmailVerified(false);
		user.signUp(this, new SaveListener() {

			@Override
			public void onSuccess() {
				T.show(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT);
				App.BmobUser = user;
				finish();
				startActivity(new Intent(RegisterActivity.this,
						PersonalInfoActivity.class));
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ErrorCodeUtil.Errorshow(RegisterActivity.this, arg0, arg1);

			}
		});
	}

	private void showDialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("提示");
		builder.setMessage("您还没注册,确定返回?");
		builder.setNegativeButton("确定",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						startActivity(new Intent(RegisterActivity.this,
								LoginActivity.class));
						finish();
					}
				});
		builder.setPositiveButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			showDialog();
			break;
		case R.id.btn_register:
			register();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showDialog();
		}
		return super.onKeyDown(keyCode, event);
	}

}
